<template>
  <div class="login">
    <div class="box-login">
      <div class="login-title"></div>
      <el-form
        ref="loginRuleFormRef"
        :model="loginForm"
        :rules="loginRules"
        validate-on-rule-change
        label-width="0"
        style="max-width: 460px"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
            size="large"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            :type="blink ? 'text' : 'password'"
            size="large"
          >
            <template #suffix>
              <el-icon v-if="!blink" @click="blinkHandler"><Hide /></el-icon>
              <el-icon v-else @click="blinkHandler"><View /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login(loginRuleFormRef)"
            style="width: 100%"
            type="primary"
            size="large"
          >
            登录
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="goToRegister" type="primary" link>
            去注册
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { User, Lock, Unlock, View, Hide } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { loginRequest } from '../api/user.js'
import qs from 'qs'
const router = useRouter()
const route = useRoute()

const loginRuleFormRef = ref()
const loginForm = reactive({
  username: route.query?.username ?? '',
  password: route.query?.password ?? '',
})
const loginRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 11, message: '用户名必须是3~11位字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入确认密码', trigger: 'blur' },
    { min: 6, message: '密码必须是大于6位的字符', trigger: 'blur' },
  ],
})
const blink = ref(false)

// 密码显示与隐藏
const blinkHandler = () => {
  blink.value = !blink.value
}

// 跳转到注册页
const goToRegister = () => {
  router.push({
    name: 'register',
  })
}

// 登录
const login = async (loginRuleFormRef) => {
  if (!loginRuleFormRef) return
  await loginRuleFormRef.validate(async (valid, fields) => {
    if (valid) {
      const params = qs.stringify({
        username: loginForm.username,
        password: loginForm.password,
      })
      const { data: res } = await loginRequest(params)
      // console.log('登录请求', res)
      const { status, message, token } = res
      if (status !== 0) return ElMessage.error(message)
      // 将token存到浏览器本地
      localStorage.setItem('token', token)
      ElMessage.success(message)

      // 跳转到首页
      router.push({ name: 'home' })
    } else {
      ElMessage.error('校验不通过!')
    }
  })
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;

  min-height: 100vh;
  background: url('../assets/images/login_bg.jpg') center center / cover
    no-repeat;

  .box-login {
    width: 400px;
    border-radius: 3px;
    padding: 0 20px;
    background-color: #fff;
  }

  .login-title {
    height: 60px;
    background: url('../assets/images/title.png') center center no-repeat;
  }
}
</style>
